package com.zhiwei.msf.common.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.zhiwei.msf.common.constant.HttpConstants;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${spring.application.name:项目名}")
    private String title;

    @Value("${swagger.version:版本号}")
    private String version;

    @Value("${swagger.description:项目描述}")
    private String description;

    @Value("${swagger.service.url:项目地址}")
    private String serviceUrl;

    @Value("${swagger.contact.name:作者}")
    private String author;

    @Value("${swagger.contact.url:作者网页}")
    private String url;

    @Value("${swagger.contact.email:作者邮箱}")
    private String email;

    /**
     * 全局Swagger配置
     *
     * @return
     */
    @Bean
    public Docket globalSwaggerConfig() {

        //请求头添加Token参数
        ParameterBuilder httpHeaderToken = new ParameterBuilder();
        httpHeaderToken.name(HttpConstants.HEADER_TOKEN)
                .description(HttpConstants.HEADER_TOKEN_DESC)
                .modelRef(new ModelRef("string"))
                .parameterType(HttpConstants.HEADER)
                .required(false)
                .build();

        ParameterBuilder httpOriginToken = new ParameterBuilder();
        httpOriginToken.name(HttpConstants.HEADER_ORIGIN)
                .description(HttpConstants.HEADER_ORIGIN_DESC)
                .modelRef(new ModelRef("string"))
                .parameterType(HttpConstants.HEADER)
                .required(false)
                .build();

        List<Parameter> globalParameters = new ArrayList<>();
        globalParameters.add(httpHeaderToken.build());
        globalParameters.add(httpOriginToken.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getSwaggerApiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(globalParameters);
    }

    /**
     * 设置Swagger页面元素
     *
     * @return
     */
    private ApiInfo getSwaggerApiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .version(version)
                .description(description)
                .termsOfServiceUrl(serviceUrl)
                .contact(new Contact(author, url, email))
                .build();
    }
}